<style lang="less">
@import "@/styles/table-common.less";
.btn-box{
  // padding: 10px 12px 10px 0;
  height: 32px;
  display: flex;
  align-items: center;
  span{
    margin-left: 15px;
  }
}
.detail-modal{
  .form{
    div{
      display: flex;
      margin-bottom: 10px;
      span{
        width: 130px;
      }
      input{
        flex: 1;
        border: 1px solid #e8eaec;
        outline: none;
        text-indent: 16px;
        color: #515a6e;
      }
    }
  }
  .refund-num{
    margin-top: 15px;
  }
}

</style> 
<template>
  <div class="search">
    <Card>
      <Row class="operation">
        <Button @click="add" type="primary" icon="md-add">添加</Button>
      </Row>
      <Form ref="searchForm" :model="searchForm" inline :label-width="100">
        <Row v-show="openSearch" @keydown.enter.native="handleSearch">
          
          <FormItem label="规则名称：">
            <Input
              type="text"
              v-model="searchForm.title"
              clearable
              placeholder="请输入会规则名称"
              style="width: 200px"
            />
          </FormItem>
          <FormItem label="规则选择：">
            <Select v-model="searchForm.cleanType" transfer placeholder="请选择" style="width: 200px">
              <Option v-for="item in rouleList" :value="item.value" :key="item">{{ item.title }}</Option>
            </Select>
          </FormItem>
          <FormItem style="margin-left: -35px" class="br">
            <Button @click="handleSearch" type="primary" icon="ios-search"
              >搜索</Button
            >
            <Button @click="handleReset">重置</Button>
          </FormItem>
        </Row>
      </Form>
      
      <Table
        :loading="loading"
        border
        :columns="columns"
        :data="data"
        sortable="custom"
        ref="table"
      >
        <template slot-scope="{ row }" slot="validTime">
          <!-- <div>{{row.siteValidTimes}}</div> -->
          <div v-if="row.siteValidTimes">
            <!-- <div v-for="item in row.siteValidTimes" :key="item.id">{{item.validStartDate}}-{{item.validEndDate}}</div> -->
            <div v-for="item in row.siteValidTimes" :key="item.id">{{item.delFlag}}</div>
          </div>
        </template>
      </Table>
      <Row type="flex" justify="end" class="page">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
      
    </Card>
    <addEdit
      :data="formData"
      :type="showType"
      v-model="showUser"
      @on-submit="getDataList"
    />
    <!-- <Modal
        v-model="modal"
        title="详情"
        @on-ok="ok"
        @on-cancel="cancel">
        <div class="detail-modal">
        </div>
        <div slot="footer">
        </div>
    </Modal> -->
  </div>
</template>

<script>
import {
  scIntegralCleanupdStatusById,

  getScIntegralCleanByCondition
} from "@/api/index";

import { shortcuts } from "@/libs/shortcuts";

import addEdit from "./addEdit.vue";
export default {
  name: "user-manage",
  components: {
    addEdit,
  },
  data() {
    return {
      message: "",
      height: 510,
      showUser: false,
      showType: "0",
      loading: true,
      selectDate: '',
      openSearch: true,
      idenList: [], //角色状态
      options: {
        shortcuts: shortcuts,
      },
      rouleList: [
        {value: '1', title: '固定周期'},
        {value: '2', title: '延续周期'},
      ],
      searchForm: {
        pageNumber: 1,
        pageSize: '10',
        sort: "createTime",
        order: "desc",
      },
      columns: [
        {
          title: "ID",
          key: "id",
          minWidth: 220,
          align: "center",
          fixed: "left"
        },
        {
          title: "规则名称",
          key: "title",
          width: 220,
          align: "center",
        },
        {
          title: "规则选择",
          key: "cleanType",
          width: 200,
          align: "center",
          render: (h, params) => {
            let txt = ''
            if(params.row.cleanType == '1'){
              txt = '固定周期'
            }else if(params.row.cleanType == '2'){
              txt = '延续周期'
            }
            return h('span', txt)
          }
        },
        {
          title: "清零时间",
          key: "cleanDay",
          width: 220,
          align: "center",
        },
        {
          title: "规则描述",
          key: "description",
          minWidth: 220,
          align: "center",
        },
        {
          title: "创建时间",
          key: "createTime",
          width: 200,
          align: "center",
        },
        {
          title: "启用状态",
          key: "status",
          width: 140,
          align: "center",
          render: (h, params) => {
            if (params.row.status == 0) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "success",
                    text: "启用",
                  },
                }),
              ]);
            } else if (params.row.status == -1) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "error",
                    text: "禁用",
                  },
                }),
              ]);
            }
          },
          // filters: [
          //   {
          //     label: "启用",
          //     value: 0,
          //   },
          //   {
          //     label: "禁用",
          //     value: -1,
          //   },
          // ],
          // filterMultiple: false,
          filterRemote: (e) => {
            let v = "";
            if (e.length > 0) {
              v = e[0];
            }
            this.searchForm.status = v;
            this.searchForm.pageNumber = 1;
            this.getDataList();
          },
        },
        {
          title: "操作",
          key: "createTime",
          width: 130,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            let enableOrDisable = "";
            if (params.row.status == 0) {
              enableOrDisable = h(
                "a",
                {
                  on: {
                    click: () => {
                      this.chageable(params.row, -1);
                    },
                  },
                },
                "禁用"
              );
            } else {
              enableOrDisable = h(
                "a",
                {
                  on: {
                    click: () => {
                      this.chageable(params.row, 0);
                    },
                  },
                },
                "启用"
              );
            }
            return h("div", [
              enableOrDisable,
              // h("Divider", {
              //   props: {
              //     type: "vertical",
              //   },
              // }),
              // h(
              //   "a",
              //   {
              //     on: {
              //       click: () => {
              //         this.seeting()
              //         this.formData = params.row
              //       },
              //     },
              //   },
              //   "设置"
              // )
            ]);
          },
        },
        
      ],
      modal: false,
      data: [],
      formData: {}
    };
  },
  methods: {
    init() {
      this.getDataList();
    },
   changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    getDataList() {
      // 多条件搜索用户列表
      this.loading = true;
      getScIntegralCleanByCondition(this.searchForm).then(res => {
        this.loading = false
        if (res.success) {
          this.data = res.result.content;
          this.total = res.result.totalElements;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      })
    },
   
    add() {
      this.showType = "2";
      this.showUser = true;
    },
    seeting() {
      // console.log('seeting')
      this.showType = "1";
      this.showUser = true;
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    selectDateRange(v) {
      if (v) {
        this.searchForm.startDate = v[0];
        this.searchForm.endDate = v[1];
      }
    },
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    handleReset() {
      this.selectDate = ''
      this.searchForm.title = ''
      this.searchForm.cleanType = ''
      this.form={
        pageNumber:1,
        pageSize:10
      }
      // 重新加载数据
      this.getDataList();
    },
    enable(v) {
      this.$Modal.confirm({
        title: "确认启用",
        content: "您确认要启用用户 " + v.title + " ?",
        loading: true,
        onOk: () => {
          enableUser(v.id).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        },
      });
    },
    chageable(v, status) {
      let statusTxt = status == 0 ? '启用' : '禁用'
      this.$Modal.confirm({
        title: `确认${statusTxt}`,
        content: `您确认要${statusTxt}` + v.title + " ?",
        loading: true,
        onOk: () => {
          scIntegralCleanupdStatusById({...v, status}).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        },
      });
    },
    cancel() {

    },
    ok() {

    }
  },
  created() {
  },
  mounted() {
    // 计算高度
    this.height = Number(document.documentElement.clientHeight - 230);
    this.init();
  },
};
</script>